package com.wondedDream.service.impl;

import com.wondedDream.model.Practice;
import com.wondedDream.model.User;
import com.wondedDream.model.UserPracticeRecord;
import com.wondedDream.repository.MockExamRepository;
import com.wondedDream.repository.PracticeQuestionRepository;
import com.wondedDream.repository.PracticeRepository;
import com.wondedDream.service.PracticeService;
import com.wondedDream.service.UserPracticeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class PracticeServiceImpl implements PracticeService {

    @Autowired
    private MockExamRepository mockExamRepository;
    @Autowired
    private PracticeRepository practiceRepository;
    @Autowired
    private PracticeQuestionRepository practiceQuestionRepository;

    @Autowired
    private UserPracticeRecordService userPracticeRecordService;


    @Override
    public Practice savePractice(Practice practice) {
        return practiceRepository.save(practice);
    }

    @Override
    public void deletePractice(Integer id) {
        practiceRepository.deleteById(id);
    }

    @Override
    public Practice updatePractice(Practice practice) {
        return practiceRepository.saveAndFlush(practice);
    }

    @Override
    public Practice getPracticeById(Integer id) {
        Practice practice = practiceRepository.findById(id).orElse(null);
        Integer practiceSize  = practiceQuestionRepository.countByPracticeId(id);
        practice.setPracticeSize(practiceSize);
        return practice;
    }

    @Override
    public List<Practice> getPracticesByCategoryIdAndOpenId(Integer categoryId, String userOpenId) {
        List<Practice> practiceList = practiceRepository.findByCategoryId(categoryId);
        List<UserPracticeRecord> practiceIds = userPracticeRecordService.findDistinctPracticeIdsByOpenid(userOpenId);
        if(practiceIds == null || practiceIds.size() == 0){
            return practiceList;
        }
        Map<Integer, Date> practiceIdToCreateTimeMap = practiceIds.stream()
                .filter(upr -> upr.getId()!= null && upr.getCreateTime()!= null)
                .collect(Collectors.toMap(UserPracticeRecord::getId, UserPracticeRecord::getCreateTime));
        for (Practice practice : practiceList) {
            Integer practiceId = practice.getId();
            if (practiceIdToCreateTimeMap.containsKey(practiceId)) {
                practice.setUserLastOperateTime(practiceIdToCreateTimeMap.get(practiceId));
            }
        }
        return practiceList;

    }

    @Override
    public List<Practice> getPracticesByCategoryId(Integer categoryId) {
        return practiceRepository.findByCategoryId(categoryId);
    }

    @Override
    public List<Practice> getAllPractices() {
        return practiceRepository.findAll();
    }

    @Override
    public void createPracticeQuestionByFileJson() {

    }
}